import java.io.*;
import java.util.Scanner;
import java.net.Socket;


public class TexasSender {
	private Socket s = null;
	public static void main(String[] args) {
		TexasSender sender = new TexasSender();
	}
	public TexasSender(){
		//message("Hi from the Sender");
	}
	public void blastCards(HandCards hc){
		String messageType = "deal_cards";
		String messageContents = hc.toString();
		String message = constructMessage(messageType,messageContents);
		message(message);
		
	}
	public void takeTurn(Turn t){
		String messageType = "turn";
		String messageContents = t.toString();
		String message = constructMessage(messageType,messageContents);
		message(message);
	}
	public String constructMessage(String type, String contents){
		return type+":{"+contents+"}";
	}
	private void message(String message){
		try {
			s = new Socket("localhost", 8888);
			//Scanner in = new Scanner(System.in);
			PrintWriter writer = new PrintWriter(s.getOutputStream());
			System.out.println("Connection to " + "192.168.0.1" + " is open");
			StringBuilder msg = new StringBuilder();
			//System.out.println("Begin message; type ! by itself when done");
			msg.append(message);
			writer.print(msg.toString());
			writer.flush();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		} finally {
			try {
				if (s != null) {s.close();}
			} catch (IOException ioe) {
				System.out.println("error closing socket");
			}
		}
	}
}